//
//  WZPSettingViewController.m
//  WZPItem
//
//  Created by 王王喆平 on 16/7/5.
//  Copyright © 2016年 UI. All rights reserved.
//

#import "WZPSettingViewController.h"
#import <SDImageCache.h>
#import "WZPFileManager.h"

#define WZPCaChePath [NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) firstObject]

@interface WZPSettingViewController()

@property (nonatomic,assign) NSInteger totalSize;

@end
@implementation WZPSettingViewController

-(void)viewDidLoad{
    [super viewDidLoad];
    
    // 设置导航条内容
    [self settingView];
    // 获取文件尺寸
    // 获取cachepath 缓存路径
    NSString * caChePath = WZPCaChePath;
    // 获取文件尺寸
    _totalSize = [WZPFileManager getSizeOfDirectoryPath:caChePath];
    
}

-(void)settingView{
  
    // 设置非根号控制器的标题
   self.title = @"设置";
   // 注册ID
    [self.tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"ID"];

}

-(void)back{
    
    [self.navigationController popViewControllerAnimated:YES];

}

#pragma mark - Table View data source 

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    
    return 1;
    
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{

    UITableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:@"ID" forIndexPath:indexPath];
    
    cell.textLabel.text = [self getSizeStr:_totalSize];
    
    return cell;
    }

// 点击cell时调用
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
 
    // 清除缓存
    [WZPFileManager deleteOfFilePath:WZPCaChePath];
    // 刷新表格
    _totalSize = 0;
    [self.tableView reloadData];
}
-(NSString *)getSizeStr:(NSInteger)totalSize{
    NSString * sizeStr = @"清除缓存";
    CGFloat totalSizeF = 0;
    if (_totalSize >= 1000 * 1000) {
        // MB  电脑上 1MB = 1000K = 10000B
        totalSizeF = _totalSize / (1000 * 1000);
        sizeStr = [NSString stringWithFormat:@"%@(%.1fMB)",sizeStr,totalSizeF];
    } else if (_totalSize >= 1000) {
        // KB  电脑上 1K = 1000B
        totalSizeF = _totalSize / 1000;
        sizeStr = [NSString stringWithFormat:@"%@(%.1fKB)",sizeStr,totalSizeF];
    } else if (_totalSize) {
        // MB  电脑上 1MB = 1000K = 10000B
        sizeStr = [NSString stringWithFormat:@"%@(%.1ldB)",sizeStr,_totalSize];
    }
    NSLog(@"%ld",totalSize);
    sizeStr = [sizeStr stringByReplacingOccurrencesOfString:@".0" withString:@""];
    return sizeStr;
    
}




@end
